package com.gopay.common.log.collect;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.domain.log.OperLogAssistDtl;
import com.gopay.common.domain.log.OperLogBankRouteDtl;
import com.gopay.common.log.constants.LogOperStatusCd;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.LoadBalancingUtils;
import com.gopay.common.util.RequestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志包裹
 *
 * @LogBag.java
 * @author dak.wang
 * @2016年4月20日 上午10:12:19 www.gopay.com.cn Inc.All rights reserved.
 */
public class LogBag {

    private String id;
    private String platform;// 系统平台(01:console02:portal)
    private String category;// 目录(01:商品02:客户03:不动账交易04:动账交易05:风控06:系统07:内管)
    /*模块
    01：加油卡02：手机流量03：手机话费04：基本信息05：业务配置
    06：安全认证07：工单处理08：差错处理09：收款10：付款
    11：转账12：退款13：基础配置14：银行配置15：事前控制
    16：事中控制17：事后控制18：用户权限
    */
    private String modules;
    //private LogOTBaseVO otBaseVO;// 操作类型
    private String threadId = Thread.currentThread().getName();// 线程ID
    private String remoteIp;// 客户端IP
    private String localIp;// 本地IP
    private String operTs = DateUtils.getCurrDate();// 操作时间
    private String snapshotBefore;// 操作前快照
    private String snapshotAfter;// 操作后快照
    private String remark;// 备注（异常或自定义提示）
    private String status = LogOperStatusCd.FAILED.value;// 状态（1-成功、2-失败）
    private String operDesc;// 操作详情

    /**** add by sh.jiao 将LogOTBaseVO中的属性搬迁到logBag中 begin****/
    private String operTypeCd;
    private String launchCustId; // 发起人
    private String launchCustName;
    private String launchUserId;
    private String launchUserName;
    private String targetCustId; // 目标人
    private String targetCustName;
    private String targetUserId;
    private String targetUserName;
    private String stepCode;// 步骤号
    private String stepName;// 步骤名称
    private String orderId;
    private String operId;

    //密码控件表ID或临时ID
    private Long pwdCtrlAttrInfoId;

    private OperLogAssistDtl assistDtl;
    private OperLogBankRouteDtl bankRouteDtl;


    /**** add by sh.jiao 将LogOTBaseVO中的属性搬迁到logBag中 end****/

    private boolean hasTransactionalAnno;

    private boolean peeked;

    /**** modify by sh.jiao 20170510 修改logBag初始化方法 begin****/
    /*public void init(HttpServletRequest request, LogOTBaseVO otBase) {
        setRemoteIp(RequestUtils.getRemoteRealIpAddr(request));
        setLocalIp(request.getLocalAddr() + "-" + LoadBalancingUtils.getRealIp());
        this.otBaseVO = otBase;

    }*/
    public void init(HttpServletRequest request) {
        setRemoteIp(RequestUtils.getRemoteRealIpAddr(request));
        String realIp = LoadBalancingUtils.getRealIp();
        if (null != realIp && !"".equals(realIp)) {
            setLocalIp(request.getLocalAddr() + "-" + LoadBalancingUtils.getRealIp());
        } else {
            setLocalIp(request.getLocalAddr());
        }
    }
    /**** modify by sh.jiao 20170510 修改logBag初始化方法 end****/
    /*public LogOTBaseVO getOtBaseVO() {
        return otBaseVO;
    }*/

    public String getThreadId() {
        return threadId;
    }

    public void setThreadId(String threadId) {
        this.threadId = threadId;
    }

    public String getRemoteIp() {
        return remoteIp;
    }

    public void setRemoteIp(String remoteIp) {
        this.remoteIp = remoteIp;
    }

    public String getLocalIp() {
        return localIp;
    }

    public void setLocalIp(String localIp) {
        this.localIp = localIp;
    }

    public String getOperTs() {
        return operTs;
    }

    public void setOperTs(String operTs) {
        this.operTs = operTs;
    }

    public String getSnapshotBefore() {
        return snapshotBefore;
    }

    public void setSnapshotBefore(String snapshotBefore) {
        this.snapshotBefore = snapshotBefore;
    }

    public String getSnapshotAfter() {
        return snapshotAfter;
    }

    public void setSnapshotAfter(String snapshotAfter) {
        this.snapshotAfter = snapshotAfter;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getOperDesc() {
        return operDesc;
    }

    public void setOperDesc(String operDesc) {
        this.operDesc = operDesc;
    }

    public boolean isHasTransactionalAnno() {
        return hasTransactionalAnno;
    }

    public void setHasTransactionalAnno(boolean hasTransactionalAnno) {
        this.hasTransactionalAnno = hasTransactionalAnno;
    }

    public boolean isPeeked() {
        return peeked;
    }

    public void setPeeked(boolean peeked) {
        this.peeked = peeked;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String toMaskJsonString(){
        try {
            String jsonVo = JSONObject.toJSONString(this, SerializerFeature.WriteMapNullValue);
            JSONObject jsonObj = JSONObject.parseObject(jsonVo);
            /*****logx 2.0 zhoutong 20170707
            if(jsonObj.containsKey("otBaseVO")){
                jsonObj.put("otBaseVO", JSONObject.parseObject(otBaseVO.toMaskJsonString()));
            }
             **/
            if(jsonObj.containsKey("snapshotBefore")){
                jsonObj.put("snapshotBefore", LoggerMaskUtils.maskString(snapshotBefore));
            }
            if(jsonObj.containsKey("snapshotAfter")){
                jsonObj.put("snapshotAfter", LoggerMaskUtils.maskString(snapshotAfter));
            }
            String resVo = JSONObject.toJSONString(jsonObj, SerializerFeature.WriteMapNullValue);
            return resVo;
        } catch(Exception e){
            return null;
        }
    }
    public String getOperTypeCd() {
        return operTypeCd;
    }

    public void setOperTypeCd(String operTypeCd) {
        this.operTypeCd = operTypeCd;
    }

    public String getLaunchCustId() {
        return launchCustId;
    }

    public void setLaunchCustId(String launchCustId) {
        this.launchCustId = launchCustId;
    }

    public String getLaunchCustName() {
        return launchCustName;
    }

    public void setLaunchCustName(String launchCustName) {
        this.launchCustName = launchCustName;
    }

    public String getLaunchUserId() {
        return launchUserId;
    }

    public void setLaunchUserId(String launchUserId) {
        this.launchUserId = launchUserId;
    }

    public String getLaunchUserName() {
        return launchUserName;
    }

    public void setLaunchUserName(String launchUserName) {
        this.launchUserName = launchUserName;
    }

    public String getTargetCustId() {
        return targetCustId;
    }

    public void setTargetCustId(String targetCustId) {
        this.targetCustId = targetCustId;
    }

    public String getTargetCustName() {
        return targetCustName;
    }

    public void setTargetCustName(String targetCustName) {
        this.targetCustName = targetCustName;
    }

    public String getTargetUserId() {
        return targetUserId;
    }

    public void setTargetUserId(String targetUserId) {
        this.targetUserId = targetUserId;
    }

    public String getTargetUserName() {
        return targetUserName;
    }

    public void setTargetUserName(String targetUserName) {
        this.targetUserName = targetUserName;
    }

    public String getStepCode() {
        return stepCode;
    }

    public void setStepCode(String stepCode) {
        this.stepCode = stepCode;
    }

    public String getStepName() {
        return stepName;
    }

    public void setStepName(String stepName) {
        this.stepName = stepName;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getOperId() {
        return operId;
    }

    public void setOperId(String operId) {
        this.operId = operId;
    }

    public OperLogAssistDtl getAssistDtl() {
        return assistDtl;
    }

    public void setAssistDtl(OperLogAssistDtl assistDtl) {
        this.assistDtl = assistDtl;
    }

    public OperLogBankRouteDtl getBankRouteDtl() {
        return bankRouteDtl;
    }

    public void setBankRouteDtl(OperLogBankRouteDtl bankRouteDtl) {
        this.bankRouteDtl = bankRouteDtl;
    }

    public String getModules() {
        return modules;
    }

    public void setModules(String modules) {
        this.modules = modules;
    }

    public Long getPwdCtrlAttrInfoId() {
        return pwdCtrlAttrInfoId;
    }

    public void setPwdCtrlAttrInfoId(Long pwdCtrlAttrInfoId) {
        this.pwdCtrlAttrInfoId = pwdCtrlAttrInfoId;
    }
}
